package com.example.demo.controller;

import com.example.demo.entity.Book;
import java.util.List;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.security.access.prepost.PreAuthorize;

@Controller
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public String listBooks(@RequestParam(value = "keyword", required = false) String keyword, Model model) {
        List<Book> books;
        if (keyword != null && !keyword.isEmpty()) {
            books = bookService.searchBooks(keyword);
        } else {
            books = bookService.findAll();
        }
        model.addAttribute("books", books);
        return "books";
    }

    @GetMapping("/new")
    @PreAuthorize("hasRole('ADMIN')")
    public String showAddForm(Model model) {
        model.addAttribute("book", new Book());
        return "book-form";
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public String addBook(@ModelAttribute Book book, RedirectAttributes redirectAttributes) {
        int result = bookService.save(book);
        if (result == -1) {
            redirectAttributes.addFlashAttribute("errorMessage", "ISBN 为 " + book.getIsbn() + " 的书籍已存在，无法重复添加。");
            return "redirect:/books/new"; // Redirect back to the add form
        } else {
            redirectAttributes.addFlashAttribute("successMessage", "书籍添加成功！");
            return "redirect:/books/new";
        }
    }

    @GetMapping("/edit/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public String showEditForm(@PathVariable Long id, Model model) {
        Book book = bookService.findById(id);
        model.addAttribute("book", book);
        return "book-form";
    }

    @PostMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    public String updateBook(@ModelAttribute Book book, @RequestParam(value = "keyword", required = false) String keyword, RedirectAttributes redirectAttributes) {
        bookService.save(book);
        if (keyword != null && !keyword.isEmpty()) {
            redirectAttributes.addAttribute("keyword", keyword);
            return "redirect:/books";
        } else {
            return "redirect:/books";
        }
    }

    @GetMapping("/delete/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public String deleteBook(@PathVariable Long id, @RequestParam(value = "keyword", required = false) String keyword, RedirectAttributes redirectAttributes) {
        bookService.deleteById(id);
        if (keyword != null && !keyword.isEmpty()) {
            redirectAttributes.addAttribute("keyword", keyword);
            return "redirect:/books";
        } else {
            return "redirect:/books";
        }
    }


}